import 'package:flutter/material.dart';
import 'musicItem.dart';

class MusicListScreen extends StatefulWidget {
  final AlbumList albumList;

  const MusicListScreen({super.key, required this.albumList});

  @override
  State<MusicListScreen> createState() => _MusicListScreen();
}

class _MusicListScreen extends State<MusicListScreen> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My Music',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text(widget.albumList.albumName,
            style: TextStyle(
              fontSize: 30,
              fontWeight: FontWeight.bold,
            ),
          ),
          actions: [
            IconButton(
              icon: const Icon(Icons.add),
              onPressed: () {},
            ),
            IconButton(
              icon: const Icon(Icons.delete),
              onPressed: () {},
            ),
          ],
        ),
        body: ListView(
          children: buildMusicList(),
        ),
      ),
    );
  }

  List<Widget> buildMusicList() {
    List<Widget> musicWidgets = [];

    for (int i = 0; i < widget.albumList.musicList.length; i++) {
      var music = widget.albumList.musicList[i];
      int index = i + 1; // 序号从1开始

      musicWidgets.add(
          GestureDetector(
            onTap: () {
              print("点击歌曲：${music.musicName}");
            },
            child: Card(
              elevation: 4,
              margin: EdgeInsets.symmetric(horizontal: 16, vertical: 2),
              child: Container(
                height: 70, // 将高度从100降低到70
                child: Row(
                  children: [
                    // 序号显示部分
                    Container(
                      width: 60, // 稍微减小宽度以匹配新高度
                      height: 60, // 稍微减小高度以匹配新高度
                      child: Center(
                        child: Text(
                          index.toString(),
                          style: TextStyle(
                            fontSize: 18, // 稍微减小字体大小
                            fontWeight: FontWeight.bold,
                            color: Colors.grey[700],
                          ),
                        ),
                      ),
                    ),
                    SizedBox(width: 12), // 稍微减小间距
                    Expanded(
                      child: Padding(
                        padding: EdgeInsets.symmetric(vertical: 6), // 减少垂直内边距
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: [
                            Text(
                              music.musicName,
                              style: TextStyle(
                                fontSize: 18, // 稍微减小字体大小
                                fontWeight: FontWeight.w500,
                              ),
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                            ),
                            Text(
                              music.musicAuthor,
                              style: TextStyle(
                                fontSize: 14, // 稍微减小字体大小
                                color: Colors.grey[600],
                              ),
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                            )
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          )
      );
    }

    return musicWidgets;
  }
}